package pt;

/**
 * @author wcj
 * @Date 2021/7/6 22:51
 * @Version 1.0
 */
public class CircleList {

    /**
     * 检查链表是否有环，如果有环，返回入环节点；否则，返回null
     *
     * @param head
     * @return
     */
    public static ListNode pt(ListNode head) {
        ListNode slow = head;
        ListNode quick = head;
        while (quick!=null && quick.next != null) {
            slow = slow.next;
            quick = quick.next.next;
            if (slow == quick) {
                ListNode tmp = head;
                while (tmp != slow) {
                    tmp = tmp.next;
                    slow = slow.next;
                }
                return tmp;
            }
        }
        return null;
    }
}

class ListNode {
    int val;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
